﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetcodeTest._100._40
{
    public class Leetcode36
    {
        /*
         36. Valid Sudoku
         Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. 

        There are just 3 rules to Sudoku.
        1.Each row must have the numbers 1-9 occuring just once.
        2.Each column must have the numbers 1-9 occuring just once.
        3.And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid
         */

        public bool IsValidSudoku(char[,] board)
        {
            List<string> listRow = new List<string>();
            List<string> listColumn = new List<string>();
            List<string> listGrid = new List<string>();
            List<char> tempListChar = new List<char>();
            char tempChar;
            int tempSubGridIndex = 0;
            for (int rowIndex = 0; rowIndex < board.GetLength(0); rowIndex++)
            {
                listRow.Add("");
                for (int columnIndex = 0; columnIndex < board.GetLength(1); columnIndex++)
                {
                    if (rowIndex == 0)
                    {
                        listColumn.Add("");
                        listGrid.Add("");
                    }
                    tempChar = board[rowIndex, columnIndex];
                    listRow[rowIndex] += tempChar;
                    listColumn[columnIndex] += tempChar;

                    tempSubGridIndex = (columnIndex / 3) + (rowIndex / 3) * 3;
                    listGrid[tempSubGridIndex] += tempChar;
                }
                if (!CheckRule(listRow[rowIndex], tempListChar))
                {
                    return false;
                }
                if ((rowIndex + 1) % 3 == 0)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (!CheckRule(listGrid[rowIndex - i], tempListChar))
                        {
                            return false;
                        }
                    }
                }
            }

            foreach (string str in listColumn)
            {
                if (!CheckRule(str, tempListChar))
                {
                    return false;
                }
            }
            return true;
        }

        private bool CheckRule(string str, List<char> tempListChar)
        {
            tempListChar.Clear();
            foreach (char a in str)
            {
                if (a != '.')
                {
                    if (tempListChar.Contains(a))
                        return false;
                    tempListChar.Add(a);
                }
            }
            //英语不好害死人。。原来都可以为空。
            //if (tempListChar.Count == 0)
            //    return false;
            return true;
        }
    }
}
